"""
title:客户端生成订单Page
author:刘鑫海
date：2022/1/6
"""

from Common.Base import Base, open_browser
import time

url = "http://localhost:8080/ecshop/"


class GenerateOrder(Base):
    """封装表现层:制作定位器"""
    smart_camera_loc = (
    'css selector', 'body>div.index-body>div>div>div:nth-child(2)>div.goods-right>div>a:nth-child(1)')  # 智能相机
    purchase_quantity_loc = ('id', 'number')  # 购买数量输入框
    buy_now_loc = ('class name', 'td1')  # 立即购买按钮
    settlement_loc = (
    'css selector', 'body>div:nth-child(7)>div:nth-child(3)>table>tbody>tr>td:nth-child(2)>a')  # 去结算按钮
    username_loc = ('name', 'username')  # 用户名输入框
    password_loc = ('name', 'password')  # 密码输入框
    login_loc = ('name', 'login')  # 登录按钮

    no_login_loc = ('class name', 'bnt_blue_2')  # 不登录按钮
    province_loc = ('id', 'selProvinces_0')  # 选择省份下拉框
    city_loc = ('id', 'selCities_0')  # 选择城市下拉框
    district_loc = ('id', 'selDistricts_0')  # 选择区域下拉框
    consignee_name_loc = ('id', 'consignee_0')  # 收货人姓名输入框
    detailed_address_loc = ('id', 'address_0')  # 详细地址输入框
    telephone_loc = ('id', 'tel_0')  # 电话输入框
    email_address_loc = ('id', 'email_0')  # 电子邮件地址输入框
    postal_code_loc = ('id', 'zipcode_0')  # 邮政编码输入框
    mobile_phone_loc = ('id', 'mobile_0')  # 手机输入框
    distribution_location_loc = (
    'css selector', '#theForm > div > table > tbody > tr:nth-child(5) > td > input.bnt_blue_2')  # 配送至这个地址按钮
    bank_remittance_loc = (
    'css selector', '#paymentTable > tbody > tr:nth-child(2) > td:nth-child(1) > input[type=radio]')  # 银行汇款方式选项

    surface_mail_loc = (
    'css selector', '#shippingTable > tbody > tr:nth-child(3) > td:nth-child(1) > input[type=radio]')  # 邮局平邮选项
    balance_payment_loc = (
    'css selector', '#paymentTable > tbody > tr:nth-child(2) > td:nth-child(1) > input[type=radio]')  # 余额支付选项
    bank_payment_loc = (
    'css selector', '#paymentTable > tbody > tr:nth-child(3) > td:nth-child(1) > input[type=radio]')  # 银行支付选项
    boutique_packaging_loc = (
    'css selector', '#packTable > tbody > tr:nth-child(3) > td:nth-child(1) > input[type=radio]')  # 精品包装选项
    blessing_card_loc = (
    'css selector', '#cardTable > tbody > tr:nth-child(3) > td:nth-child(1) > input[type=radio]')  # 祝福贺卡选项
    blessing_words_loc = ('css selector', '#cardTable > tbody > tr:nth-child(4) > td:nth-child(3) > textarea')  # 祝福语输入框
    invoice_loc = ('css selector', '#ECS_NEEDINV')  # 开发票选项
    invoice_type_loc = ('id', 'ECS_INVTYPE')  # 发票类型下拉框
    ifnvoice_header_loc = ('id', 'ECS_INVPAYEE')  # 发票抬头输入框
    postscript_loc = ('id', 'postscript')  # 订单附言输入框
    place_order_loc = (
    'css selector', '#theForm > div:nth-child(16) > div:nth-child(3) > input[type=image]:nth-child(1)')  # 提交订单按钮
    user_center_loc = ('css selector', 'body > div:nth-child(7) > div > p > a:nth-child(2)')  # 用户中心按钮
    my_order_loc = (
    'css selector', 'body > div.block.clearfix.userpage > div.AreaL > div > div > div > div > a:nth-child(3)')  # 我的订单按钮

    # 断言用
    consignee_loc = (
    'css selector', '#theForm > div:nth-child(4) > table > tbody > tr:nth-child(1) > td:nth-child(2)')  # 收货人
    amount_payable_loc = ('css selector',
                          'body > div:nth-child(7) > div > table > tbody > tr:nth-child(1) > td > strong:nth-child(3)')  # 应付金额
    order_number_loc = ('css selector', 'body > div:nth-child(7) > div > h6 > font')  # 订单号
    user_order_number_loc = ('css selector',
                             'body > div.block.clearfix.userpage > div.AreaR > div > div > div > table > tbody > tr:nth-child(1) > td:nth-child(1) > a')  # 用户中心订单号
    order_status_loc = ('css selector',
                        'body > div.block.clearfix.userpage > div.AreaR > div > div > div > table > tbody > tr:nth-child(1) > td:nth-child(4)')  # 订单状态
    total_order_amount_loc = ('css selector',
                              'body > div.block.clearfix.userpage > div.AreaR > div > div > div > table > tbody > tr:nth-child(1) > td:nth-child(3)')  # 用户中心订单金额
    insufficient_funds_loc = ('css selector', 'body > div.block > div > div > div > div > p:nth-child(1)')  # 余额不足

    """封装操作层：针对表现层中的元素操作"""

    def click_smart_camera(self):
        """
        点击智能相机商品
        :return:
        """
        self.click(self.smart_camera_loc)

    def input_purchase_quantity(self, quantity):
        """
        输入购买数量
        :param quantity: 数量
        :return:
        """
        self.clear(self.purchase_quantity_loc)
        self.send_keys(self.purchase_quantity_loc, quantity)

    def click_buy_now(self):
        """
        点击立即购物按钮
        :return:
        """
        self.click(self.buy_now_loc)

    def click_settlement(self):
        """
        点击去结算按钮
        :return:
        """
        self.click(self.settlement_loc)

    def input_username(self, username):
        """
        输入用户名
        :param username: 用户名
        :return:
        """
        self.send_keys(self.username_loc, username)

    def input_password(self, password):
        """
        输入密码
        :param password: 密码
        :return:
        """
        self.send_keys(self.password_loc, password)

    def click_login(self):
        """
        点击登录按钮
        :return:
        """
        self.click(self.login_loc)

    def click_no_login(self):
        """
        点击不登录按钮
        :return:
        """
        self.click(self.no_login_loc)

    def selects_province(self, text):
        """
        选择省份下拉框
        :param text:
        :return:
        """
        self.drop_down_list(self.province_loc, text)
        time.sleep(5)

    def selects_city(self, text):
        """
        选择城市下拉框
        :param text:
        :return:
        """
        self.drop_down_list(self.city_loc, text)
        time.sleep(5)

    def selects_district(self, text):
        """
        选择区域下拉框
        :param text:
        :return:
        """
        self.drop_down_list(self.district_loc, text)
        # time.sleep(5)

    def input_consignee_name(self, consignee_name):
        """
        输入收货人姓名
        :param consignee_name: 收货人姓名
        :return:
        """
        self.send_keys(self.consignee_name_loc, consignee_name)

    def input_detailed_address(self, detailed_address):
        """
        输入详细地址
        :param detailed_address: 详细地址
        :return:
        """
        self.send_keys(self.detailed_address_loc, detailed_address)

    def input_telephone(self, telephone):
        """
        输入电话
        :param telephone: 电话
        :return:
        """
        self.send_keys(self.telephone_loc, telephone)

    def input_email_address(self, email_address):
        """
        输入电子邮件地址
        :param email_address: 电子邮件地址
        :return:
        """
        self.send_keys(self.email_address_loc, email_address)

    def input_postal_code(self, postal_code):
        """
        输入邮政编号
        :param postal_code: 邮政编号
        :return:
        """
        self.send_keys(self.postal_code_loc, postal_code)

    def input_mobile_phone(self, mobile_phone):
        """
        输入手机
        :param mobile_phone: 手机
        :return:
        """
        self.send_keys(self.mobile_phone_loc, mobile_phone)

    def select_bank_remittance(self):
        """
        选择银行汇款方式选项
        :return:
        """
        self.click(self.bank_remittance_loc)

    def click_distribution_location(self):
        """
        点击配送至这个地址按钮
        :return:
        """
        self.click(self.distribution_location_loc)

    def select_surface(self):
        """
        选择邮局平邮选项
        :return:
        """
        self.click(self.surface_mail_loc)

    def select_balance_payment(self):
        """
        选择余额支付选项
        :return:
        """
        self.click(self.balance_payment_loc)

    def select_bank_payment(self):
        """
        选择银行支付选项
        :return:
        """
        self.click(self.bank_payment_loc)

    def select_boutique_packaging(self):
        """
        选择精品包装选项
        :return:
        """
        self.click(self.boutique_packaging_loc)

    def select_blessing_card(self):
        """
        选择祝福卡选项
        :return:
        """
        self.click(self.blessing_card_loc)

    def input_blessing_words(self, blessing_words):
        """
        输入祝福语
        :param blessing_words: 祝福语
        :return:
        """
        self.send_keys(self.blessing_words_loc, blessing_words)

    def select_invoice(self):
        """
        选择开发票选项
        :return:
        """
        self.click(self.invoice_loc)

    def selects_invoice_type(self, text):
        """
        发票类型下拉框
        :param text:
        :return:
        """
        self.drop_down_list(self.invoice_type_loc, text)

    def input_ifnvoice_header(self, ifnvoice_header):
        """
        输入发票抬头
        :param ifnvoice_header: 发票抬头
        :return:
        """
        self.send_keys(self.ifnvoice_header_loc, ifnvoice_header)

    def input_postscript(self, postscript):
        """
        输入订单附言
        :param postscript:
        :return:
        """
        self.send_keys(self.postscript_loc, postscript)

    def click_place_order(self):
        """
        点击提交订单按钮
        :return:
        """
        # time.sleep(5)
        self.click(self.place_order_loc)

    def get_amount_payable_text(self):
        """
        获取应付金额文本值
        :return:
        """
        return self.get_element_text(self.amount_payable_loc)

    def get_order_number_text(self):
        """
        获取订单号文本值
        :return:
        """
        return self.get_element_text(self.order_number_loc)

    def click_user_center(self):
        """
        点击用户中心按钮
        :return:
        """
        self.click(self.user_center_loc)

    def click_my_order(self):
        """
        点击我的订单按钮
        :return:
        """
        self.click(self.my_order_loc)

    def get_user_order_number_text(self):
        """
        获取用户中心订单号
        :return:
        """
        return self.get_element_text(self.user_order_number_loc)

    def get_order_status_text(self):
        """
        获取用户中心订单状态
        :return:
        """
        return self.get_element_text(self.order_status_loc)

    def get_total_order_amount_text(self):
        """
        获取用户中心订单金额
        :return:
        """
        return self.get_element_text(self.total_order_amount_loc)

    def get_insufficient_funds_text(self):
        """
        获取余额不足文本值
        :return:
        """
        return self.get_element_text(self.insufficient_funds_loc)

    def get_consignee_text(self):
        """
        获取收货人文本值
        :return:
        """
        return self.get_element_text(self.consignee_loc)


if __name__ == '__main__':
    generate = GenerateOrder(open_browser())
    generate.open_url(url)
    generate.click_smart_camera()
    generate.input_purchase_quantity(2)
    generate.click_buy_now()
    generate.click_settlement()
    generate.input_username("liu123456")
    generate.input_password(123456789)
    generate.click_login()

    # generate.click_no_login()
    # generate.selects_province("四川省")
    # generate.selects_city("达州市")
    # generate.selects_district("通川区")
    # generate.input_consignee_name("刘")
    # generate.input_detailed_address("魏兴镇")
    # generate.input_telephone("13333333330")
    # generate.input_email_address("2430791458@qq.com")
    # generate.input_postal_code(666666)
    # generate.input_mobile_phone("13333333333")
    # generate.click_distribution_location()

    generate.select_surface()
    generate.select_bank_payment()

    generate.select_bank_remittance()

    generate.select_boutique_packaging()
    generate.select_blessing_card()
    generate.input_blessing_words("购买商品了!!!")
    generate.select_invoice()
    generate.selects_invoice_type("2 [1.5%]")
    generate.input_ifnvoice_header("刘")
    generate.input_postscript("快点发货")
    # generate.click_place_order()
    # print(generate.get_amount_payable_text())
    # print(generate.get_order_number_text())
    # generate.click_user_center()
    # generate.click_my_order()
    # print(generate.get_user_order_number_text())
    # print(generate.get_order_status_text())
    # print(generate.get_total_order_amount_text())
